package main

import (
	"flag"
	"log"
	"magent"
	"os"
)

var runServer = flag.NewFlagSet("runserver", flag.ExitOnError)
var database = flag.NewFlagSet("database", flag.ExitOnError)

var configPath string

func init() {
	runServer.StringVar(&configPath, "config", "./", "Config file path.")
	database.StringVar(&configPath, "config", "./", "Config file path.")
}

func usage() {
	help := `
Usage of %s:
  %s COMMAND

COMMAND:

  runserver		run web server.
  database		create sqlite db and init.

`
	log.Printf(help, os.Args[0], os.Args[0])
}

func main() {
	if len(os.Args) < 2 {
		usage()
		os.Exit(1)
	}
	switch os.Args[1] {
	case "database":
		database.Parse(os.Args[2:])
		magent.CreateTable(configPath)
	case "runserver":
		runServer.Parse(os.Args[2:])
		app, err := magent.CreateApp(configPath)
		if err != nil {
			log.Panic(err)
		}
		magent.RunSched()
		app.Listen(":8888")
	default:
		usage()
	}

	// go magent.Sched(test, "10:49:00", "5s")

}
